home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlb20 / lib / signal.c < prev    next >
C/C++ Source or Header  |  1992-05-15  |  2KB  |  82 lines

  1. /* signal() for MiNT; written by ERS, placed in the public domain */
  2.  
  3. #include <errno.h>
  4. #include <osbind.h>
  5. #include <mintbind.h>
  6. #include <signal.h>
  7.  
  8. /* vector of signal handlers (for TOS, or for MiNT with -mshort) */
  9. extern __Sigfunc _sig_handler[NSIG];
  10.  
  11. /* vector giving which signals are currently blocked from delivery (for TOS) */
  12. extern long _sigmask;
  13.  
  14. /* vector giving an indication of which signals are currently pending (for TOS) */
  15. extern long _sigpending;
  16.  
  17.  
  18. #ifdef __MSHORT__
  19. /* trampoline code: for any caught signal, converts the 32 bit signal
  20.  * number MiNT passed us into a 16 bit one, and jumps to the handler
  21.  * we previously established
  22.  */
  23. void
  24. _trampoline(sig)
  25.     long sig;
  26. {
  27.     __Sigfunc func;
  28.  
  29.     func = _sig_handler[sig];
  30.  
  31. /* note: func should never be SIG_IGN or SIG_DFL; if it is, something
  32.  * really bad happened and we want to crash anyway!
  33.  */
  34.     (*func)((short)sig);
  35. }
  36. #endif
  37.  
  38. __Sigfunc
  39. signal(sig, func)
  40.     int sig;
  41.     __Sigfunc func;
  42. {
  43.     long old;
  44.     extern int __mint;
  45.     __Sigfunc oldfunc;
  46.  
  47.     if (__mint == 0) {
  48.         if (sig < 0 || sig >= NSIG) {
  49.             errno = ERANGE;
  50.             return SIG_ERR;
  51.         }
  52.         oldfunc = _sig_handler[sig];
  53.         _sig_handler[sig] = func;
  54.         return oldfunc;
  55.     }
  56.  
  57. #ifdef __MSHORT__
  58. /* NOTE: MiNT passes 32 bit numbers for signals, so we want our
  59.  * own signal dispatcher to switch these to 16 bit ints
  60.  */
  61.     if (sig < 0 || sig >= NSIG) {
  62.         errno = ERANGE;
  63.         return SIG_ERR;
  64.     }
  65.     oldfunc = _sig_handler[sig];
  66.     _sig_handler[sig] = func;
  67.     if (func != SIG_DFL && func != SIG_IGN) {
  68.         func = _trampoline;
  69.     }
  70. #endif
  71.     old = Psignal((short)sig, (long)func);
  72.     if (old < 0) {
  73.         errno = -old;
  74.         return SIG_ERR;
  75.     }
  76.     func = (__Sigfunc) old;
  77. #ifdef __MSHORT__
  78.     if (func == _trampoline) func = oldfunc;
  79. #endif
  80.     return func;
  81. }
  82.